package com.woniuxy.controllers;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.entitys.Sale;
import com.woniuxy.entitys.Users;
import com.woniuxy.services.SaleService;
import com.woniuxy.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "销售机会管理接口")
public class SaleController {
    @Autowired
    private SaleService saleService;
    @Autowired
    private UserService userService;
    @GetMapping("/sales")
    //查询所有销售机会或根据客户名称查询
    @ApiOperation(
            value = "查询销售机会",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页显示的条目数",required=true),
            @ApiImplicitParam(paramType="query",name="key",value="查询条件",required=false),
            @ApiImplicitParam(paramType="query",name="ssle_state",value="销售状态（0，1，2，3）",required=false),

    })
    public RestPOJO getAll(Integer currentPage, Integer pageSize,String key,Integer sale_state){
        RestPOJO rdt=new RestPOJO();
        //pageInfo.list:代表就是每页的数据
        PageInfo<Sale> pageInfo= saleService.selAllSale(key,currentPage,pageSize,sale_state);

        if(pageInfo!=null&&pageInfo.getList().size()>0){
            rdt.setMessage("查询数据成功");
            rdt.setStatus(200);
            rdt.setData(pageInfo);
        }
        else{
            rdt.setData(pageInfo);
            rdt.setMessage("查询数据失败");
            rdt.setStatus(10001);
        }
        return rdt;
    }
    //查询状态为未指派的所有销售机会
//    @GetMapping("/sales/state")
//    public RestPOJO sel(Integer currentPage, Integer pageSize){
//        RestPOJO rdt=new RestPOJO();
//        //pageInfo.list:代表就是每页的数据
//        PageInfo<Sale> pageInfo= saleService.selSales(currentPage,pageSize);
//        if(pageInfo!=null&&pageInfo.getList().size()>0){
//            rdt.setMessage("查询数据成功");
//            rdt.setStatus(200);
//            rdt.setData(pageInfo);
//        }
//        else{
//            rdt.setData(pageInfo);
//            rdt.setMessage("查询数据失败");
//            rdt.setStatus(10001);
//        }
//        return rdt;
//    }
    //增加销售机会
    @PostMapping("/sales")
    @ApiOperation(
            value = "创建销售机会",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    public RestPOJO save(@RequestBody Sale sale){
        RestPOJO restPOJO = new RestPOJO();
        int count=saleService.add(sale);
        if(count>0) {
            restPOJO.setMessage("增加成功");
            restPOJO.setStatus(1000);

        }
        else{
            restPOJO.setMessage("增加失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    //编辑销售机会或指派确定保存指派人和处理人
    @PutMapping("/sales")
    @ApiOperation(
            value = "编辑销售机会",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    public RestPOJO saveUpdSale(@RequestBody Sale sale){
        RestPOJO restPOJO = new RestPOJO();
        int count=saleService.upd(sale);
        if(count>0) {
            restPOJO.setMessage("编辑成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("编辑失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    //删除销售机会
    @DeleteMapping("/sales/{sale_id}")
    @ApiOperation(
            value = "删除销售机会",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="sale_id",value="销售机会id",required=true),


    })
    public RestPOJO saveUpdSale(@PathVariable("sale_id") Integer sale_id){
        RestPOJO restPOJO = new RestPOJO();
        int count=saleService.del(sale_id);
        if(count>0) {
            restPOJO.setMessage("删除成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("删除失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    //指派销售机会（查询所有客户经理）
    @GetMapping("/sales/users")
    @ApiOperation(
            value = "查询所有客户经理",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    public RestPOJO getUsersByRole_id(){
        RestPOJO restPOJO = new RestPOJO();
        List<Users> list = userService.getUsersByRole_id();
        if(list.size()>0) {
            restPOJO.setData(list);
            restPOJO.setMessage("成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessage("失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
